package com.persicadev.khayyam;

import java.util.List;

import com.persicadev.khayyam.R;
import com.persicadev.khayyam.IPoemDb.Poem;


import android.app.Activity;
import android.content.SharedPreferences;
import android.database.DataSetObserver;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

public class WidgetConfigActivity extends Activity implements OnClickListener {
	
	private SharedPreferences mSharedPreferences;
	
	private Spinner modeSpinner;
	private Spinner periodSpinner;
	
	private Typeface typeFace;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.widgetconfig);
		mSharedPreferences = getSharedPreferences(KhayyamWidget.PREFS_WIDGET, MODE_PRIVATE);
		typeFace = Typeface.createFromAsset(getAssets(), "fonts/BZar.ttf");
		TextView modeTitle = (TextView) findViewById(R.id.config_mode_text);
		modeTitle.setTypeface(typeFace);
		modeTitle.setText(PersianReshape.reshape(modeTitle.getText().toString()));
		TextView periodTitle = (TextView) findViewById(R.id.config_period_text);
		periodTitle.setTypeface(typeFace);
		periodTitle.setText(PersianReshape.reshape(periodTitle.getText().toString()));
		modeSpinner = (Spinner) findViewById(R.id.config_mode);
		modeSpinner.setAdapter(new Adapter(R.array.displaymode));
		periodSpinner = (Spinner) findViewById(R.id.config_period);
		periodSpinner.setAdapter(new Adapter(R.array.periods));
		Button add = (Button) findViewById(R.id.config_add);
		add.setTypeface(typeFace);
		add.setText(PersianReshape.reshape(add.getText().toString()));
		add.setOnClickListener(this);
	}

	public void onClick(View v) {
		switch (modeSpinner.getSelectedItemPosition()) {
		case 0:
			mSharedPreferences.edit().remove(KhayyamWidget.KEY_IDS).commit();
			break;
		case 1:
			List<Poem> results = SearchActivity.loadBookmarks(mSharedPreferences);
			String value = new String();
			for (Poem poem : results) value = value + poem.getId() + ",";
			mSharedPreferences.edit().putString(KhayyamWidget.KEY_IDS, value);
			break;
		}
		int period = 0;
		switch (periodSpinner.getSelectedItemPosition()) {
		case 0:
			period = 2*60*1000;
			break;
		case 1:
			period = 60*60*1000;
			break;
		case 2:
			period = 4*60*60*1000;
			break;
		case 3:
			period = 24*60*60*1000;
			break;
		case 4:
			period = 7*24*60*60*1000;
			break;
		case 5:
			period = 30*24*60*60*1000;
			break;
		case 6:
			period = 0;
			break;
		}
		mSharedPreferences.edit().putInt(KhayyamWidget.KEY_PERIOD, period).commit();
		KhayyamWidget.setAlarm(mSharedPreferences, getApplicationContext());
		setResult(RESULT_OK, getIntent());
		finish();
		overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
	}

	private class Adapter implements SpinnerAdapter {

		private String strings[];
		
		public Adapter(int entries) {
			strings = WidgetConfigActivity.this.getResources().getStringArray(entries);
		}
		
		public int getCount() {
			return strings.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public int getItemViewType(int position) {
			return IGNORE_ITEM_VIEW_TYPE;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			TextView view = new TextView(WidgetConfigActivity.this);
			view.setTypeface(typeFace);
			view.setText(PersianReshape.reshape(strings[position]));
			view.setTextColor(0xff000000);
			view.setGravity(Gravity.RIGHT);
			view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			return view;
		}

		public int getViewTypeCount() {
			return 1;
		}

		public boolean hasStableIds() {
			return true;
		}

		public boolean isEmpty() {
			return strings.length==0;
		}

		public void registerDataSetObserver(DataSetObserver observer) { }

		public void unregisterDataSetObserver(DataSetObserver observer) { }

		public View getDropDownView(int position, View convertView,
				ViewGroup parent) {
			CheckedTextView view = (CheckedTextView) View.inflate(WidgetConfigActivity.this, android.R.layout.simple_spinner_dropdown_item, null);
			view.setTypeface(typeFace);
			view.setText(PersianReshape.reshape(strings[position]));
			return view;
		}
		
	}
	
}
